Hospital crowdedness evaluation and in-hospital resource allocation based on image recognition technology

How to allocate the existing medical resources reasonably, alleviate hospital congestion and improve the patient experience are problems faced by all hospitals. At present, the combination of artificial intelligence and the medical field is mainly in the field of disease diagnosis, but lacks successful application in medical management. We distinguish each area of the emergency department by the division of medical links. In the spatial dimension, in this study, the waitlist number in real-time is got by processing videos using image recognition via a convolutional neural network. The congestion rate based on psychology and architecture is defined for measuring crowdedness. In the time dimension, diagnosis time and time-consuming after diagnosis are calculated from visit records. Factors related to congestion are analyzed. A total of 4717 visit records from the emergency department and 1130 videos from five areas are collected in the study. Of these, the waiting list of the pediatric waiting area is the largest, including 10,436 (person-time) people, and its average congestion rate is 2.75, which is the highest in all areas. The utilization rate of pharmacy is low, with an average of only 3.8 people using it at the one time. Its average congestion rate is only 0.16, and there is obvious space waste. It has been found that the length of diagnosis time and the length of time after diagnosis are related to age, the number of diagnoses and disease type. The most common disease type comes from respiratory problems, accounting for 54.3%. This emergency department has congestion and waste of medical resources. People can use artificial intelligence to investigate the congestion in hospitals effectively. Using artificial intelligence methods and traditional statistics methods can lead to better research on healthcare resource allocation issues in hospitals.

Medical resources are limited, and the demand for medical resources continues to increase 1 . Various countries have varying degrees of shortage of medical resources, which is more severe in developing countries [2][3][4] . Rational allocation of macro-level health resources involves regional health planning, the layout of health resources, and so on. And the micro-level involves the rational use of inventory resources, which in medical institutions includes such things as the distribution of human resources, the setting of departments, and the layout of buildings. The direct manifestation of the distribution of medical resources in a hospital is the congestion situation in the hospital 5 . In recent years, the number of daily visits in urban hospitals, especially large general hospitals, has continued to increase 1 . And various public health emergencies in cities have occurred from time to time, resulting in an increasing frequency of congestion in hospitals 6 . This not only seriously affects the quality of medical services and reduces the satisfaction of patients, but also increases the possibility of medical disputes and aggravates the conflict between doctors and patients 7 . Therefore, in order to reduce and alleviate the congestion of the hospital and improve the patient experience, the hospital is trying to reform and optimize the allocation of hospital resources 8 .
Crowding occurs when the medical services required by patients exceed the capacity that the hospital can provide. Generally, Hospital design, department layout and resource allocation affect the congestion in the hospital 9 . Investigating the congestion in the hospital and studying the specific relationship between congestion and hospital design and system planning could provide managers with effective decision-making opinions on the allocation of medical resources.
Artificial intelligence is the study of how to make computers do intelligent tasks that only humans could do. In recent years, artificial intelligence has been widely used in the medical field as a new technology that plays an important role in medical practice such as imaging assisted diagnosis and pharmaceutical exploitation 10,11 . However, there is a lack of high-tech applications related to artificial intelligence in health management. At the same time, artificial intelligence has played a huge role in the field of public management and public security. For example, monitoring the crowd gathering through surveillance video to prevent stampedes and other gathering accidents 12 .
Similar to the need to monitor crowd aggregation and prevent congestion in the field of public safety, hospital management also needs to manage congestion. In this field, the management of congestion and allocation of resources in the existing hospitals usually relies on the personal experience of the administrators, without quantitative data analysis. Manual collection of crowd data for quantitative study in hospitals requires a lot of manpower and time. When dealing with resource allocation problems, AI can have better adaptability and responsiveness than manual scheduling 13,14 . Therefore, similar to use AI to monitor crowd aggregation and prevent congestion in the field of public safety, the use of advanced computer technology to manage hospital congestion is a feasible method.
The emergency department (ED), which has a diagnosis and treatment room of general clinics, internal medicine, surgery, gynecology, pediatrics, and other specialized departments, is a relatively independent unit. And the emergency department often requires receiving a mass patient, easily forming a short peak of visits 15 . Therefore, the emergency department is a perfect research object for congestion research. In order to provide an early alert for the manager and a reference for solving the congestion in the hospital, this research aims to use artificial intelligence technology to intelligently monitor and analyze the time and location characteristics of the hospital congestion, the relationship among different medical treatment processes and its' influencing factors.

Methods
Study design and research object. This research is based on the existing hospital's treatment process and big data of the crowd to analyze the congestion of the hospital and the rationality of their resource allocation. This research focus on the spatial and temporal distribution of the density of the crowd through artificial intelligence identification, collection and analysis. We collect data from a tertiary hospital. Tertiary care institution settings are adopted in China, and tertiary hospitals being the largest hospitals with the highest technology levels. Tertiary hospitals are similarly the most congested providers while having the most complete treatment link. Studying the congestion of tertiary hospitals is beneficial to study the congestion law of each diagnosis and treatment link in medical institutions. We randomly selected a tertiary hospital. We communicate with the emergency department of this hospital and conduct an on-site inspection, so as to understand the emergency procedures in the actual operation and the areas with large people flow. And we randomly selected a week of monitoring video records from the emergency department with 168 h of video, from 26 October to 1 November 2020, including the registered hall, the waiting area of Pediatrics, the waiting area of Pharmacy, the waiting area of the Internal medicine and Surgery, the waiting area of Inspection department. In addition, we get disease diagnosis-related information, including patient's age, gender, disease diagnosis information and visit times. All information extracted does not involve basic information, such as individual names.
Data collection. In this study, we create a model of patient flow in the hospital to distinguish the various areas of the hospital. The research divides the whole process of medical treatment into three parts: Registration, Visits and Examinations, and the Post-diagnosis process. Registration is divided into online registration and onsite registration. Only the patient's arrival in the hospital is considered in the study. In this part, we collect the data from the registration hall of the hospital. The diagnosis part includes diagnostic rooms of all departments. Usually, the doctor arranges an examination for the patient after the initial consultation. And then, the patient returns to the clinic for further diagnosis after the examination. In this study, we focus on the time of initial diagnosis. Therefore, we collect the data from waiting areas of various departments in the hospital. The post-diagnosis process refers to the process after diagnosis until leaving the hospital. This part includes inspection, payment, taking medicine, etc. Based on this model, we collect three categories of information: (1) Image information: monitoring information at various nodes; (2) medical visit information, i.e. the visitor records of this emergency department; (3) Architectural information, i.e. the architectural situation in this emergency department.
Therefore, we focus on the actual scene of the emergency department and collect surveillance videos of crowded areas in the emergency department. The acquisition area to be collected includes emergency registration, the waiting area of the department of Pediatrics (the Department of Pediatrics in the emergency department is in an independent area), the department of internal medicine and Surgery, the inspection waiting area and an independent pharmacy. For each area, we collect the surveillance video of that area in one week in batches. The daily collection time of each area is the working time of the department. Of these, Pediatrics and internal medicine and Surgery are 24-h jobs, and registration offices, inspection departments, and pharmacies are from 8:00 a.m. to 6:00 p.m. Every half hour, we intercept a one-minute surveillance video. Therefore, the Pediatrics www.nature.com/scientificreports/ department and the department of internal medicine and Surgery collect 48 videos per day, while the other areas collect 21 videos per day. We collect 1113 videos in total. The videos are divided according to the acquisition area, and the videos of different areas are saved in chronological order. Because all the areas we collect are waiting areas. Most people in the video are stationary. In order to reduce the total amount of data and accelerate the data processing, we choose a relatively low sampling rate. The area of each scene is large, the shortest length of the corridor is about 8 m. We assume that the walking speed through pedestrians is 1.5 m/s, so the sampling rate of 0.2 FPS may not miss the people who are passing through. Video frame extraction is performed at Frames Per Second (FPS) = 0.2, i.e., 1 image is collected every 5 s. 10 images are collected for each video. The images are divided according to the acquisition area and chronologically deposited into different folders. Finally, we get 11,130 images for crowd counting. The data collection at this stage provides a realistic basis for the next project design.
On the other hand, we also collate the records of emergency visits. This record contains the number of patients, gender, age, outpatient diagnosis, the earliest time of receipt, the earliest time of diagnosis, the earliest time of payment, and the earliest time of dispensing medication. This record can be combined with video data to provide more detail on studying the congestion in this emergency department.
Image process based on convolutional neural network. We use a convolutional neural network (CNN) to measure the number of people. CNN is one of the most popular AI models in the field of image recognition right now. Compared with the traditional artificial neural network method, the structure of CNN is simpler and can save more computing resources. At present, CNN models can identify people with masks very well 16 .
We annotated 1000 images from the collected images as the training dataset for training our model. At first, the MATLAB 2020a has been used to annotate the training dataset. We annotated each head in the image and generated a map which has the same size as the original image. In the annotated map, the pixel value at the annotation is one and others pixel value is zero. Then, we use a Gaussian kernel with a kernel size is 15 and σ is 4 to perform Gaussian filtering on the annotated map to obtain the density map as the ground truth of our dataset. The total pixel value of the density map is the total number of people in the image.
We build a Multi-fusion convolutional neural network (MFCNN) based on the ResNet 17 and U-Net 18 to predict the number of people in the images. Our MFCNN uses the encoder-decoder structure. In the encoder, our MFCNN has an input block and four downsampling blocks. The input block condenses the image to a quarter of its original size. This can reduce the amount of memory consumed by the model at runtime. The feature image then passes through four consecutive downsampling blocks. In order from shallow to deep, these blocks gradually include more base blocks to enhance the feature extraction ability. The base block is built based on the bottleneck layer of ResNet. In decoder, the feature map in the depth would be upsampled and fused with the feature map with larger size. Finally, fine-grained regressor refines the feature map and generates a density map to predict the number of people.
In the fine-grained compressor, the input feature map is cut into strips from the width and height dimensions. Each strip is sent to a base block for fine-grained local feature extraction. This step can increase the ability of extracting the correlation between features to obtain a more complete feature image. Then, these strips are spliced into a feature image through the width and height dimensions. Feature maps with fine-grained feature enhancement in the two dimensions are fused as the final feature map. The regression layer composed of a global average pooling layer and a convolutional layer predicts the density map through the final feature map. The predicted density map is enlarged to the size of the input image by a dilated convolutional layer. We use global average pooling layer and 1 × 1 convolutional layer to replace the fully connection layer. This enables our model to adapt to various input images of different sizes.
The training process is shown in Fig. 1. In each epoch, the system records training errors and corrects the network in the next epoch. After training, the system compares the errors from each epoch and picks the network with the best results. Finally, the best network for crowd counting can help us get the number of people from images. Our experiments are based on a Python environment. Batch size is set to 8, Adam is the optimizer, and 50 epochs are learned at a fixed learning rate of 1e−5.
Statistics. Statistics on CNN model. We use two indexes to evaluate the prediction results to predict the number of people. The mean counting error (MCE), which measures instance counting accuracy for R images: where C i gt represents the ground truth number of people in the i-th picture, and C i pred is the number of people predicted by the generator. MCE can numerically represent the average number of false identifications per image. A smaller value of MCE means a smaller amount of counting error per image on average.
The root mean squared error (RMSE) can reflect the deviation value of the prediction error of each instance: where R is the number of responses, C i gt represents the ground truth number of people in the i-th picture, and C i pred is the number of people predicted by the generator. RMSE is a commonly used model performance www.nature.com/scientificreports/ evaluation metric in the field of object counting. A smaller value of RMSE means better accuracy of the model prediction.
Statistics on numbers of people. Based on the obtained crowd data, from the two perspectives of different areas at the same time and the same area at different times, the crowd information, which is collected from the images, is sorted by date. First, we identified the number of people in every image by CNN for each sampling time point. The number of people from all pictures at this sampling time point is then averaged to get the number of people at this sampling time point in one area. The average number of individuals at each site is finally counted into a table in chronological order.
Statistics on time spent. The emergency department visit records are processed to calculate the length of visit time after diagnosis (T1) and the length of diagnosis time (T2). T1 is the time of the earliest drug delivery time or the earliest payment time minus the time of pickup. T2 is the earliest time to diagnosis minus the time of pickup. After that, we extracted the influencing factors that might be associated with T1 and T2 from the records of emergency department visits: gender, age, number of diseases, and kind of diseases.
Unit congestion rate. According to Hall's Personal Space Theory of Social Psychology, personal distance is usually defined as 4 feet, or 1.2 m 19 . Therefore, we can estimate the theoretical number of people awaiting treatment in an area based on the area of this location and the personal distance, which makes the waiting crowd feel psychological discomfort, as shown in Eq. (3). Then take the ratio of the actual number of people to the theoretical capacity as the unit congestion rate in the area, as shown in Eq. (4).
Based on the unit congestion rate, it fits the curve graph of the congestion rate change over time in each position within a day; draws the fluctuation diagram of the unit congestion rate change along the medical process at each time point; draws the fluctuation diagram of the unit congestion rate change on each medical process at the same time on different days of a week.
Influence factor. In addition, we use t-test, ANOVAs test and Kruskal Wallis test to investigate the correlation of changes in visit length or diagnosis time with several influencing factors. Explored whether these influencing factors could contribute to hospital congestion by affecting the length of a visit or diagnosis time.
All analyses are performed using the social science statistics package (SPSS) version 25 (SPSS, Chicago, USA). First, the descriptive statistics (frequency, percentage, average and standard deviation [SD]) are calculated. We use T-test to compare the 2 groups. And ANOVAs test is performed for multi-group comparison to evaluate the difference of continuous variables, when the data is the normal distribution and conforms to the assumption of homogeneity of variance. In addition, the non-parameter method is adopted. When the test of variance shows heterogeneity of variance, we use the Kruskal-Wallis H test to find the relationship between groups. The probability value of P < 0.05 means statistically significant.
Ethical statement. All methods are performed in accordance with the relevant guidelines and regulations by including a statement in the "Methods" section. The acquisition of surveillance video and building data is approved by the hospital and approved by the ethics committee. The acquisition of visit records is approved by the participating hospitals and all patients. Informed consent is obtained by telephone from the participants. For patients aged < 18 years, consent is obtained from a legal guardian. The data are all processed for desensitization, and low-resolution images are used for video images. The research has approval from the ethics committee of the Shanghai Jiaotong University School of medicine.

Result
The hospital record has 4717 records, of which 1042 records can not count the length of diagnosis time or the length of visit time after diagnosis, and 3675 records are useful, accounting for 77.9% of the total.
Validation of multi fusion convolutional neural network. We use 1000 annotated images to train our multi fusion convolutional neural network. 600 images, of which 600 are used for model training and 400 for model testing. The division of test and training sets is random. Mall dataset contains 2000 images. We randomly choose 1600 images for model training and 400 for model testing.
We compared our MFCNN with some classical CNN model on our dataset and Mall dataset, which is a public dataset and has a similar scenario to our dataset. In Table 1, the experimental results show that our MFCNN has the lowest mean counting error (MCE) and root mean squared error (RMSE), which means our MFCNN has the better counting accuracy than the classical CNN model. Distribution of waiting people in surveillance video. Using a convolution neural network, we calculate the number of people waiting in the waiting area of each department, and the results are recorded in Table 2 (3) N theoretical = area For Registration, the maximum peak of waiting number in the morning occurred on Monday and it in the afternoon occurs on Friday; There is a small peak almost every morning at 11:00; The maximum peak of waiting people occurs later on weekends than it on weekdays.
For Pediatrics, the number of people waiting for treatment increase rapidly from 8:30 am to 9:30 and maintained a high level throughout the day, with a smaller decrease in waiting numbers at midday and afternoon rest times; Usually, the number of people waiting for treatment on weekend nights is smaller than that on weekday nights. For the waiting areas of Internal medicine and Surgery department, the number of people waiting for treatment is large and fluctuating.
In inspection, the first peak of waiting people on each day comes earlier than it in other departments, usually around 8:30; And the waiting number has a rapid upstroke before the emergency department knocking off at 18:00. There is not a large gap in the total reception number per day at the pharmacy, but waiting numbers in various periods of the day fluctuated greatly. Although the average waiting number in the registered area is smaller than that of Pediatrics, its maximum waiting number is close to that of Pediatrics. Crowdedness is noted in all areas except testing departments and pharmacies, where crowdedness is greatest in Pediatrics waiting areas. Table 3 and Fig. 3, based on the available data, we have listed 4 factors that may affect the length of visit time after diagnosis and diagnosis time: age, gender, number of diagnoses, disease types. Of the patients, 66% are children under ten years of age. We make separate comparisons between groups of children. The period under 1-year-old is called infancy, the period from 1 to 3 years old is called early childhood, the period from 3 to 6 years old is called preschool, and the period from 6 to 12 years old is called school age. Only one disease has been diagnosed in most patients. The most common comes from respiratory problems, accounting for 54.3%. Table 4, we obtain the architectural data for this emergency in the diagnostic area through building drawings and field surveys and we calculate the Optimal number of people waiting for treatment for the area based on Personal Space Theory 19 . Among them, the registered lobby has the largest building area. And Monday and Friday have the highest number of doctors arranged.

Discussion
It is important to optimize the allocation of health resources. Current tertiary hospitals in China also commonly suffer from the problems of 'three long and one short' (long registration time, long waiting time, long time to pick up drugs, and short visiting time). Through the study of hospital congestion, it is helpful for managers to understand the flow of patients in the hospital. Thus, it can improve the configuration of resources and the visiting service of hospitals. www.nature.com/scientificreports/ The survey shows that the burden in Pediatrics is substantial. Pediatrics had the largest waiting list and the greatest patient density. The waiting number of Pediatrics rose rapidly during the morning hours from 8 to 9 am and is maintained around 40 thereafter. In terms of the congestion rate, the average congestion rate of Pediatrics is 2.75 19 . In fact, the pediatric environment is also chaotic, and the affected children often cry loudly. This environment would aggravate the anxiety of patients and their families and thus reduce the patient's visiting experience and satisfaction 22 . Additionally, the emphasis placed on the next generation in Chinese traditional culture has been easily magnified by the parents' anxiety from the malaise of child visits, which further worsens the diagnosis and treatment experience. Poor experience of patient may lead to decreased medical effectiveness and even causes Doctor-patient Conflict 23,24 . Increasing the number of physicians to reduce the peak waiting number might be an effective way to improve their experience. However, at present, pediatrics is a subject that doctors are reluctant to choose. The lack of availability of pediatricians has become increasingly significant. Government and hospitals should improve the treatment of pediatrics to increase the number of pediatricians. Additionally, providing possible length of waiting time can help to enhance the visitor's experience 25,26 . At the same time, expanding the area of the waiting region to reduce the density of the waiting people may improve the patient's experience. Moreover, improving the environment of pediatric, including recreational imaging, wallpaper color, children's activity areas and animation to divert the child's attention, might reduce the crying of the child and relieve the anxiety of the child's family 22 .
The medical examination is an important link in the modern medical process and inspection reports are the important basis for physicians to initiate diagnosis and treatment. It should be noticed that the distribution of waiting people in the Inspection shows a dumbbell type distribution: high at both heads and low in the middle. The first peak of waiting number in the Inspection comes earlier than it in other departments, which is usually at 8:30, while the others are at 9:30. In addition, the number of waiting people at the Inspection is rise before offhours, which may indicate that patients are eager to complete the medical test before off hours to avoid dragging the test to the next day. Previous studies have shown that sampling examinations are required for the majority of   www.nature.com/scientificreports/ patients, and the waiting time for sampling examinations is long 27 . In Table 3, concurrence of multiple diseases does not result in a longer diagnostic time for physicians, but significantly increases the length of time used after diagnosis. Because multiple diseases are possible, the patient is referred for multiple medical tests to further define the type and circumstances of the disease. So doctors need to rely on laboratory examinations to detect the patient's condition, and complex diseases spend significantly more time on inspection to be determined like Oncology cancer 28 . The speed of examination constrains the speed of the whole visit process. The main reason for the patients waiting in the queue at the Inspection department is waiting for samples collection. Too long waiting time could affect the fluency of all visit progress and cause patient dissatisfaction 22,29 . Faced with the above problems, on the one hand, hospitals may implement a shift by improving the scheduling mechanism, changing the scheduling time of the laboratory department, increasing the number of workers in the morning and offhours, or by adding a shift to reduce the waiting time of patients in the queue and avoid the centralized outbreak of inspection needs. On the other hand, the hospital could improve testing devices 30,31 . The use of more advanced equipment can reduce the time required for a single inspection. In addition, by conducting a health economics analysis of devices' effectiveness, hospitals can comprehensively judge how much unit time inspection efficiency and hospital operation efficiency have been improved by the introduction of high-efficiency equipment 32 . Meanwhile, pharmacies with very low crowding rates also deserve attention. As can be seen in Table 4, the independent emergency pharmacy is less frequently used but occupies a significant amount of space. Other studies have shown that pharmacies are indeed a vulnerable, wasteful and inefficient link in hospitals 33 . The emergency pharmacy could be combined with the outpatient department pharmacy. In this way, the hospital carries on the unified planning to the pharmacy, reduces resources waste, and improves the working efficiency 34 . On the other hand, hospitals can cooperate with outside drugstores. So that some patients can choose to buy drugs in drugstores. This is helpful in shortening the total time of patients in the hospital and reduce hospital expenses (Supplementary Information).
In Fig. 3, there is a clear positive correlation trend between patient age and length of diagnosis time. This may be related to disease complexity in elder patients 35 . Expressive skills and memory are relatively poor in the elderly compared to the young 36,37 . Physicians may spend more time communicating to determine the type of condition and disease development when faced with an elder patient. For the elder patients, we can strengthen the role of the family doctors, let them join the consultation and make use of well-established case records so that the patient's appeal can be more clearly expressed 38,39 . Nevertheless, younger patients generally spend more time after diagnosis than elder patients. Because most elder patients are Geriatric disorders or Chronic diseases patients, and they regularly visit the hospital for diagnosis 40 . Therefore, these patients are generally more familiar with the department setting and visit process, while young patients may not be familiar enough. In response to the issue of inexperienced access for young people, we suggest that an introduction to the visit process can be provided on the appointment software to help young people understand the visiting process in the hospital and the location of each department. On the other hand, the hospital can set some striking signals or electronic navigation, which facilitates patients to find destinations quickly 41 .
In addition, respiratory diseases occupy more than 50% of the patients in the emergency department and most of them are influenza patients. This leads to congestion in the emergency department and a waste of medical resources. Most influenza patients could choose to go to community hospitals rather than tertiary hospitals. From other studies, we found that this is a common phenomenon that patients are more inclined to seek treatment in tertiary hospitals rather than community hospitals 15 . We believe that the local government also needs to strengthen the construction of community hospitals, strengthen the training of family doctors, and improve the hierarchical diagnosis and treatment system 39,42 . Through community hospitals, family doctors and other means, to relieve the crowdedness of central hospitals and to reduce the time-consuming of patients 43 .

Conclusion
This study presents a summary analysis of congestion at a tertiary hospital through a survey based on crowd counting via CNN. We propose a process of medical treatment based on hospital visit flow to divide the regions of the hospital. Meanwhile, we propose the unit congestion rate, which is used to calculate the crowdedness of a location. To count the number of waiting people, we propose the multi fusion convolutional neural network. Through the statistical analysis of the monitoring data by artificial intelligence, we validate the congestion existing at this hospital. Based on this, we propose improvements in the allocation of medical resources to the hospital. In addition, this study finds associations of age, disease category with the length of diagnosis time, and length of time after diagnosis. And we also find a relation between the number of diagnoses and length of time after diagnosis. These empirical data also help us to make improved opinions on the allocation of medical resources in this hospital and local medical policies for the local government.
However, the sample size is not large enough due to the short period of data acquisition. Some possible periodic features may fail to be detected. And our failure to access the appointment registration to face-to-face visits, so we are not able to conduct a more complete study of the whole visit flow. In the future, we will conduct more research to better play the role of AI in medical management.

Data availability
The datasets use and/or analyse during the current study are available from the corresponding author on reasonable request.